package com.yanqu.road.server.timer.dataanalytics;

import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.server.manger.maidian.UserBagGoodsChangeMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class DataAnalyticsTask extends GeneralTask {

    private Logger logger = LogManager.getLogger(DataAnalyticsTask.class.getName());

    private long lastLoadTime = LocalDateTimeHelper.getZeroTimeTimeStamp();

    private int lastFlushTime = DateHelper.getCurrentSecond();

    @Override
    public void doRun() {

        try {
            long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
            long serverId = ConfigHelper.getLong("serverId");
            //按区服分时间上报，不要都集中在0点
            long gap = serverId % 5;
            gap = gap * 1000;
            if (lastLoadTime < zeroTime + gap) {
                lastLoadTime = zeroTime + gap ;

                try{
                    DataAnalyticsMgr.resetDayOnlineInfo();
                }catch (Exception e){
                    logger.error("", e);
                }

                //榜单快照, 先去掉
                //DataAnalyticsMgr.trackUserRankEvent();

                try{
                    for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
                        //0点事件
                        DataAnalyticsMgr.traceDayLogin(player, zeroTime);
                    }
                }catch (Exception e){
                    logger.error("", e);
                }
                //---------------------- 移动到每日首次登录
//                //背包快照
//                BuyItemLog.trackUserBagSnapshot();
//
//                //上传门客信息快照
//                PatronsLog.trackPatronsInfoSnapshot();
//
//                //上传美女信息快照
//                BeautyLog.trackBeautyInfoSnapshot();
//
//                //上传珍兽信息快照
//                PetLog.trackPetInfoSnapshot();
//
//                //鱼快照
//                ManorFishLog.trackAllFishInfoSnapshot();
//
//                //蛐蛐快照
//                CricketLog.trackCricketSnapshot();
//
//                //财神快照
//                MammomLog.trackSnapshot();

                //---------------------- 移动到每日首次登录
            }
        }catch (Exception e){
            logger.error("trackUserRankEvent task error:", e);
        }

        try {
            int openDays = ServerListMgr.getServerOpenDay(ConfigHelper.getLong("serverId"));
            int gap = 10;
            //新服活跃，缩小间隔，10S传2000条数据
            if(openDays > 0 && openDays <= 30){
                gap = 5;
            }
            if (DateHelper.getCurrentSecond() - lastFlushTime > gap) {
                DataAnalyticsMgr.flush();
                lastFlushTime = DateHelper.getCurrentSecond();
            }
        }catch (Exception e){
            logger.error("DataAnalyticsMgr flush task error:", e);
         }


        //3点或者4点
        int hour = DateHelper.getHours();
        long serverId = ConfigHelper.getLong("serverId");
        if(hour == 3 && serverId % 2 == 1){
            UserBagGoodsChangeMgr.traceLastDayUserBagChange();
        }else if(hour == 4 && serverId % 2 == 0){
            UserBagGoodsChangeMgr.traceLastDayUserBagChange();
        }
    }
}
